package inbound_invoice

import (
	"context"
	"fmt"
	"net/http"
	"wms/model"

	"wms/internal/svc"
	"wms/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type InboundInvoiceUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInboundInvoiceUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InboundInvoiceUpdateLogic {
	return &InboundInvoiceUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InboundInvoiceUpdateLogic) InboundInvoiceUpdate(req *types.InboundInvoiceUpdateRequest) (resp *types.BaseResponse, err error) {
	resp = new(types.BaseResponse)

	//1.供货商是否存在
	supplier, err := l.svcCtx.SupplierModel.FindOne(l.ctx, req.SupplierId)
	switch err {
	case nil:
	case model.ErrNotFound:
		fmt.Printf("[Error]供货商[%d]不存在\n", req.SupplierId)
		resp.Code = http.StatusBadRequest
		resp.Msg = "供货商不存在"
		return resp, nil
	default:
		fmt.Printf("[Error]查询供货商[%d]:%s\n", req.SupplierId, err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	//2.更新开票记录
	err = l.svcCtx.InboundInvoiceModel.Update(l.ctx, &model.InboundInvoice{
		Id:           req.Id,
		SupplierId:   supplier.Id,
		SupplierName: supplier.Name,
		Total:        req.Total,
		Tax:          req.Tax,
		Date:         req.Date,
	})
	switch err {
	case nil:
	default:
		fmt.Printf("[Error]更新开票记录:%s\n", err.Error())
		resp.Code = http.StatusInternalServerError
		resp.Msg = "服务内部错误"
		return resp, nil
	}

	resp.Code = http.StatusOK
	resp.Msg = "成功"
	return resp, nil
}
